package com.qzdsoft.erpcloud.service.com.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.com.CustomerSupplierStatement;
import com.qzdsoft.erpcloud.mapper.com.CustomerSupplierStatementMapper;
import com.qzdsoft.erpcloud.mapper.com.CustomerSupplierStatementQueryMapper;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementInfo;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementQueryParam;
import com.qzdsoft.vo.Response;

@Service
public class CustomerSupplierStatementServiceImpl implements CustomerSupplierStatementService
{
	 private static final Logger logger = LoggerFactory.getLogger(CustomerSupplierStatementServiceImpl.class);
    @Autowired
    private CustomerSupplierStatementQueryMapper customerSupplierStatementQueryMapper;
    @Autowired
    private CustomerSupplierStatementMapper customerSupplierStatementMapper;
    
    
    @Override
    public List<CustomerSupplierStatementInfo> selectBy(Integer companyid, Integer customerSupplierid)
    {
        return customerSupplierStatementQueryMapper.selectBySupplierCustomerId(companyid, customerSupplierid);
    }

    
    @Override
    public CustomerSupplierStatement selectById(Integer id) {
        return customerSupplierStatementMapper.selectByPrimaryKey(id);
    }


    @Override
    public Response edit( CustomerSupplierStatement comCustomerSupplierStatement) {
    	int count = customerSupplierStatementQueryMapper.selectDefaultCount(comCustomerSupplierStatement.getCompanyId(), comCustomerSupplierStatement.getSupplierCustomerId());
        if(null != comCustomerSupplierStatement.getId()){
        	//  编辑
        	CustomerSupplierStatement obj = customerSupplierStatementMapper.selectByPrimaryKey(comCustomerSupplierStatement.getId());
        	if(obj.getIsDefault() !=1) {
        		if(count>0 && comCustomerSupplierStatement.getIsDefault() == 1) {
            		logger.error("结算方式编辑新增错误:已有默认");
            		throw new RuntimeException("已有默认结算方式，请重新选择！");
            	}
        	}
            int ui = customerSupplierStatementMapper.updateByPrimaryKeySelective(comCustomerSupplierStatement);
            return Response.success("编辑成功");
        } else {
        	if(count>0 && comCustomerSupplierStatement.getIsDefault() == 1) {
        		logger.error("结算方式编辑新增错误:已有默认");
        		throw new RuntimeException("已有默认结算方式，请重新选择！");
        	}
            comCustomerSupplierStatement.setCtime(new Date());
            customerSupplierStatementMapper.insert(comCustomerSupplierStatement);
            return Response.success("新增成功");
        }
    }

    @Override
    public Response del(Integer id) {
    	customerSupplierStatementMapper.deleteByPrimaryKey(id);
        return Response.success(null);
    }


	@Override
	public List<CustomerSupplierStatementInfo> selectAll(CustomerSupplierStatementQueryParam info) {
		PageHelper.startPage(info.getPage(), info.getLimit());
		List<CustomerSupplierStatementInfo> list = customerSupplierStatementQueryMapper.selectAll(info);
		return list;
	}


    
    
    
}
